const log4js = require('koa-log4');
const logger = log4js.getLogger('routes::staff');
var querystring = require('querystring');
const router = require('koa-router')();
exports = module.exports = router;
const mongodb = require('mongodb');
const ObjectId = mongodb.ObjectId;
const template = require('art-template');

const _ = require('lodash');
const util = require(global.prefixPath + '/commons/util');
const site_service = require(global.prefixPath + '/services/site');
const info_service = require(global.prefixPath + '/services/info');
const city_service = require(global.prefixPath + '/services/city');
const categories = require(global.prefixPath + '/data/industries.json');

router.get('/:city/list', async (ctx, next) => {
    try {
        // 推广部分
        let fields = {adsenses: 1, _id: 0};
        let adsenses = (await site_service.findById(ctx.site._id, fields)).adsenses;
        let allids = [];
        for (let key in adsenses) {
            allids = allids.concat(adsenses[key]);
        }
        allids = util.toObjectId(allids);

        let allads = await info_service.findByIds(allids);

        // 组合数据
        for (let key in adsenses) {
            let ads = _.filter(allads, (val) => {
                return _.indexOf(adsenses[key], val._id.toString()) >= 0;
            });
            adsenses[key] = ads;
        }
        ctx.state.adsenses = adsenses;
        // 推广 end


        let params = querystring.parse(ctx.req._parsedUrl.query);
        if (params.query) {
            console.log('list 输出 params.query =')
            console.log(params.query);
            let querysource = JSON.parse(params.query);
            querysource.site = ctx.site._id.toString();
            for (var key in querysource) {
                if (querysource[key]) {
                    if (key == 'category' || key == 'estate.type') {
                        querysource[key] = {$in: querysource[key]}
                    }
                    if (key.indexOf('estate.transferFee') != -1) {
                        querysource[key] = {$lte: querysource[key]}
                    }
                    if (key.indexOf('estate.rental') != -1 || key.indexOf('estate.area') != -1) {
                        querysource[key] = {$gte: querysource[key][0], $lte: querysource[key][1]}
                    }
                } else {
                    delete querysource[key];
                }
            }
            console.log('list 输出 querysource =')
            console.log(querysource);
            console.log('----------- querysouurce -------------')
            let q = {
                query: querysource,
                sort: params.sort ? JSON.parse(params.sort) : {
                    createAt: -1
                },
                page: params.page ? JSON.parse(params.page) : {
                    index: 1,
                    size: 10
                }
            }
            let info_result = await info_service.find(q);
            console.log('------------- info-result -----------')
            console.log(info_result);
            console.log('------------- info-result end -----------')
            console.log(params)
            ctx.state.pub_info = info_result;
            ctx.state.query_info = params;
            ctx.state.query_state = JSON.parse(params.query);

        } else {
            //  let info_result = await info_service.findAll();
            let info_result = await info_service.findAll();
            ctx.state.pub_info = info_result;
        }

    } catch (err) {
        throw err;
    }
    return await ctx.render('list/index');
});
